package com.zxlfysj.kruskal;


/**
 * 克鲁斯卡尔算法实例
 *
 * @author yangshujing
 * @create 2020-08-26 15:39
 */
public class KruskalAlgorithm {
    private int edgeNum; //边的个数
    private char[] vertexs; //顶点数组
    private int[][] matrix; //邻接矩阵
    private static final int INF = Integer.MAX_VALUE;   //使用整数最大值表示两个顶点不能连通

    public static void main(String[] args) {
        char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        //表示邻接矩阵的二维数组
        //INF表示不连通，0表示顶点本身
        int[][] matrix = new int[][]{
                {0, 12, INF, INF, INF, 16, 14},
                {12, 0, 10, INF, INF, 7, INF},
                {INF, 10, 0, 3, 5, 6, INF},
                {INF, INF, 3, 0, 4, INF, INF},
                {INF, INF, 5, 4, 0, 2, 8},
                {16, 7, 6, INF, 2, 0, 9},
                {14, INF, INF, INF, 8, 9, 0},
        };
        KruskalAlgorithm kruskalAlgorithm = new KruskalAlgorithm(vertexs, matrix);
        kruskalAlgorithm.kruskalAlgorithm();

    }

    //构造器
    public KruskalAlgorithm(char[] vertexs, int[][] matrix) {
        int vlen = vertexs.length;
        this.vertexs = new char[vlen];
        for (int i = 0; i < vertexs.length; i++) {
            this.vertexs[i] = vertexs[i];
        }
        this.matrix = new int[vlen][vlen];
        for (int i = 0; i < vlen; i++) {
            for (int j = 0; j < vlen; j++) {
                this.matrix[i][j] = matrix[i][j];
            }
        }

        //统计边的条数
        for (int i = 0; i < vlen; i++) {
            for (int j = i + 1; j < vlen; j++) {
                //统计边
                if (matrix[i][j] != INF) {
                    edgeNum++;
                }
            }
        }

    }

    public void kruskalAlgorithm() {
        int index = 0;
        int[] ends = new int[edgeNum];  //生成最小生成树过程中各顶点的终点
        EData[] res = new EData[edgeNum]; //保存最小生成树的结果
        //获取边的集合
        EData[] edges = getEdges();
        sortEdges(edges);   //排序
        //判断准备加入的边是否构成回路，没有就加入res结果中
        for (int i = 0; i < edgeNum; i++) {
            int p1 = getPosition(edges[i].start);
            int p2 = getPosition(edges[i].end);
            //获取p1在已有生成树的终点
            int m = getEnd(ends, p1);
            int n = getEnd(ends, p2);
            if (m != n) { //没有构成回路
                ends[m] = n;
                res[index++] = edges[i];
            }
        }

        //输出最小生成树
        for (int i = 0; i < index; i++) {
            System.out.println(res[i]);
        }

    }

    //显示邻接矩阵
    public void print() {
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = 0; j < vertexs.length; j++) {
                System.out.printf("%12d\t", matrix[i][j]);
            }
            System.out.println();
        }
    }

    //对边进行排序
    private void sortEdges(EData[] edges) {
        for (int i = 0; i < edges.length - 1; i++) {
            for (int j = 0; j < edges.length - 1 - i; j++) {
                if (edges[j].weight > edges[j + 1].weight) {
                    EData temp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = temp;
                }
            }
        }
    }

    //获取顶点字符的下标
    private int getPosition(char ch) {
        for (int i = 0; i < vertexs.length; i++) {
            if (vertexs[i] == ch) {
                return i;
            }
        }
        return -1;
    }

    //通过邻接矩阵获取所有边，放到数组里面，以便进行遍历
    private EData[] getEdges() {
        int index = 0;
        EData[] edges = new EData[edgeNum];
        for (int i = 0; i < vertexs.length; i++) {
            for (int j = i + 1; j < vertexs.length; j++) {
                if (matrix[i][j] != INF) {
                    edges[index++] = new EData(vertexs[i], vertexs[j], matrix[i][j]);
                }
            }
        }
        return edges;
    }

    /**
     * 获取下标i的顶点的终点，用于判断两个顶点的终点是否相同，若相同就构成回路
     *
     * @param ends 各个顶点对应的终点数组
     * @param i
     * @return
     */
    public int getEnd(int[] ends, int i) {
        while (ends[i] != 0) {
            i = ends[i];
        }
        return i;
    }
}

//表示边的对象
class EData {
    char start; //边的一个点
    char end;   //边的另一个点
    int weight; //边的权值

    public EData(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "EData{" +
                "<" + start +
                ", " + end +
                ">=" + weight +
                '}';
    }
}
